package com.automannn.atm_back.config.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 * @author chenkh
 * @time 2020/7/17 23:16
 */
public class AtmBooleanDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonToken currentToken = jp.getCurrentToken();

        if (currentToken.equals(JsonToken.VALUE_TRUE) || currentToken.equals(JsonToken.VALUE_FALSE)) {
            String text = jp.getText();

            if ("true".equals(text)) {
                return "1";
            } else if ("false".equals(text)) {
                return "0";
            }else {
                return text;
            }
        }
        return jp.getText();
    }
}
